package com.example.myapplication.activity;

import android.app.Dialog;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.Switch;

import androidx.appcompat.app.AppCompatActivity;

import com.alibaba.fastjson.JSONObject;
import com.example.myapplication.bean.LotDevice;
import com.example.myapplication.common.IAMUserConfig;
import com.example.myapplication.common.http.MyHttp;
import com.example.myapplication.common.http.MyHttpCallback;
import com.example.myapplication.util.LoadingUtil;

/**
 * 设备控制界面基类
 */
public abstract class BaseActivity extends AppCompatActivity implements MyHttpCallback {

    //获取设备属性
    protected static final int GET_DEVICE_PROPERTIES = 0;
    //目标设备
    LotDevice lotDevice;

    private Dialog loadingDialog;

    private String device_url;
    protected MyScheduleTask myScheduleTask;

    //网络请求处理
    protected MyHttp myHttp;

    //处理返回数据消息
    protected abstract void doHttpData(Message message);

    //配置服务ID
    protected abstract String serviceID();

    //布局ID
    protected abstract int layoutID();

    //初始化布局控件
    protected abstract void initView();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(layoutID());
        initView();
        //获取设备属性
        initData();
    }

    private void initData() {
        lotDevice = (LotDevice) getIntent().getSerializableExtra("device");
        myHttp = new MyHttp(this, this);
        getDeviceProps();
    }

    /**
     * 获取设备属性
     * 以AMQP协议方式维持与华为云物联网平台设备属性上报消息推送
     */
    private void getDeviceProps() {
        loadingDialog = LoadingUtil.createLoadingDialog(this);
        loadingDialog.show();
        device_url = "https://iotda.cn-north-4.myhuaweicloud.com/v5/iot/" + IAMUserConfig.project_id +
                "/devices/" + lotDevice.getDevice_id() + "/properties?service_id=" + serviceID();
        myHttp.queryHttpGet(device_url, GET_DEVICE_PROPERTIES);
    }

    /**
     * 控制点击下发命令监听器
     */
    class MyControlListener implements View.OnClickListener {

        private String commnad_name;
        private String command_param;
        private int requestCode;

        //传入控制设备参数
        public MyControlListener(String commnad_name, String command_param, int requestCode) {
            this.commnad_name = commnad_name;
            this.command_param = command_param;
            this.requestCode = requestCode;
        }

        @Override
        public void onClick(View v) {
            Switch toggle = ((Switch) v);
            //控制按钮不可用
            toggle.setEnabled(false);
            //下发设备命令
            String url = "https://iotda.cn-north-4.myhuaweicloud.com/v5/iot/" + IAMUserConfig.project_id + "/devices" +
                    "/" + lotDevice.getDevice_id() + "/commands";
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("service_id", serviceID());
            jsonObject.put("command_name", commnad_name);
            JSONObject object = new JSONObject();
            object.put(command_param, toggle.isChecked() ? "ON" : "OFF");
            jsonObject.put("paras", object);
            String jsonParams = jsonObject.toString();
            Log.d("zoubo", toggle.getText().toString() + ":" + jsonParams);
            myHttp.queryHttpPost(url, jsonParams, requestCode);
        }
    }

    /**
     * 定时任务：查询设备属性
     */
    class MyScheduleTask implements Runnable {

        @Override
        public void run() {
            myHttp.queryHttpGet(device_url, GET_DEVICE_PROPERTIES);
        }
    }

    @Override
    public void doMessage(Message message) {
        //关闭加载框
        loadingDialog.dismiss();
        doHttpData(message);
    }

    /**
     * 获取内部handler
     *
     * @return
     */
    public Handler getMyHttpHandler() {
        return myHttp.getmHandler();
    }


    @Override
    protected void onDestroy() {
        //结束轮询
        getMyHttpHandler().removeCallbacks(myScheduleTask);
        super.onDestroy();
    }
}